﻿using AutoMapper;
using MediatR;
using Notification.API.Application.Controller.Notification;
using Notification.API.DTOs.Notifications;
using Notification.Errorcode.ErrorCode;
using Notification.Infrasturcture.Interfaces;

namespace Notification.API.Application.CommandHandler.Notifications
{
    public class SelectNotificationCommandHandler:IRequestHandler<SelectNotificationCommand,ApiResult<NotificationDTOs>>
    {
        private readonly INotificationRepository notificationRepository;

        private readonly IMapper mapper;

        public SelectNotificationCommandHandler(INotificationRepository notificationRepository, IMapper mapper)
        {
            this.notificationRepository = notificationRepository;
            this.mapper = mapper;
        }

        public async Task<ApiResult<NotificationDTOs>> Handle(SelectNotificationCommand request, CancellationToken cancellationToken)
        {
            // 获取所有生产工单
            var Productionorders = await notificationRepository.GetAsync(x => x.Productid == request.Productid);

            // 使用 AutoMapper 进行对象映射
            var productionorderPlanResponses = mapper.Map<NotificationDTOs>(Productionorders);


            // 返回成功结果
            return ApiResult<NotificationDTOs>.Success(productionorderPlanResponses, StatusCodeEnum.Success);
        }
    }
}
